#include "DUICtrlBase.h"

//////////////////////////////////////////////////////////////////////////
// CDUIBase
//////////////////////////////////////////////////////////////////////////
CDUIBase::CDUIBase(void)
{
	m_pResList	= NULL;
}

CDUIBase::~CDUIBase(void)
{
	for (UINT ii = 0; ii < m_nResCount; ii++)
	{
		if (NULL != m_pResList[ii])
		{
			DeleteObject(m_pResList[ii]);
		}
	}

	if (NULL != m_pResList)
	{
		delete[] m_pResList;
	}
}

void CDUIBase::SetRect(int x, int y, int cx, int cy)
{
	m_ptPos.x	= x;
	m_ptPos.y	= y;
	m_szSize.cx	= cx;
	m_szSize.cy	= cy;
}

void CDUIBase::SetTexture(UINT* nResIDs, UINT nResCount)
{
	if (NULL != m_pResList)
	{
		delete[] m_pResList;
	}

	m_nResCount	= nResCount;
	m_pResList	= new HBITMAP[nResCount];
	for (UINT ii = 0; ii < nResCount; ii++)
	{
		m_pResList[ii] = LoadBitmap(GetModuleHandle(NULL), MAKEINTRESOURCE(nResIDs[ii]));	
	}

}

//////////////////////////////////////////////////////////////////////////
// CDUICtrlBase
//////////////////////////////////////////////////////////////////////////
CDUICtrlBase::CDUICtrlBase(HWND hParent, UINT nID)
{
	m_hParent	= hParent;
	m_nCtrlID	= nID;
}

CDUICtrlBase::~CDUICtrlBase(void)
{

}

//////////////////////////////////////////////////////////////////////////
// CDUICanvas
//////////////////////////////////////////////////////////////////////////
CDUICanvas::CDUICanvas()
{

}

CDUICanvas::~CDUICanvas()
{

}

void CDUICanvas::Draw(HDC& hDC)
{
	if (NULL != m_pResList && NULL != hDC)
	{
		HDC hMemDC = CreateCompatibleDC(hDC); 
		SelectObject(hMemDC, m_pResList[0]);
		BitBlt(hDC, m_ptPos.x, m_ptPos.y, m_szSize.cx, m_szSize.cy, hMemDC, 0, 0, SRCCOPY);
		DeleteDC(hMemDC);
	}
}

//////////////////////////////////////////////////////////////////////////
// CDUIButton
//////////////////////////////////////////////////////////////////////////
CDUIButton::CDUIButton(HWND hParent, UINT nID) : CDUICtrlBase(hParent, nID)
{
	m_pResList	= NULL;
	m_nCurSel	= 0;
	m_bVisible	= true;
	m_bBtnDown	= false;
	m_bEnable	= true;
}

CDUIButton::~CDUIButton()
{

}

void CDUIButton::Draw(HDC& hDC)
{
	if (NULL != m_pResList && NULL != hDC && NULL != m_pResList && m_bVisible)
	{
		HDC hMemDC = CreateCompatibleDC(hDC); 
		SelectObject(hMemDC, m_pResList[m_nCurSel]);
		BitBlt(hDC, m_ptPos.x, m_ptPos.y, m_szSize.cx, m_szSize.cy, hMemDC, 0, 0, SRCCOPY);
		DeleteDC(hMemDC);
	}

	return;
}

void CDUIButton::Enable(bool bEnable)
{
	m_bEnable	= bEnable;
}

void CDUIButton::Visible(bool bVisible)
{
	m_bVisible	= bVisible;

	RECT rtArea;
	::SetRect(&rtArea, m_ptPos.x, m_ptPos.y, m_ptPos.x + m_szSize.cx, m_ptPos.y + m_szSize.cy);
	InvalidateRect(m_hParent, &rtArea, TRUE);
}

bool CDUIButton::CursorInCtrl(LPARAM lParam)
{
	RECT rtArea;
	::SetRect(&rtArea, m_ptPos.x, m_ptPos.y, m_ptPos.x + m_szSize.cx, m_ptPos.y + m_szSize.cy);
	bool bMouseCtrl = false;
	POINT ptMouse = {LOWORD(lParam), HIWORD(lParam)};
	return (TRUE == PtInRect(&rtArea, ptMouse));
}

void CDUIButton::Redraw()
{
	RECT rtArea;
	::SetRect(&rtArea, m_ptPos.x, m_ptPos.y, m_ptPos.x + m_szSize.cx, m_ptPos.y + m_szSize.cy);
	InvalidateRect(m_hParent, &rtArea, TRUE);
}

bool CDUIButton::OnMessageHandle(UINT nMessage, WPARAM wParam, LPARAM lParam)
{
	bool bHandle = false;
	switch (nMessage)
	{
		case WM_LBUTTONDOWN:
		{
			if (CursorInCtrl(lParam) && m_bEnable)
			{
				bHandle		= true;
				m_nCurSel	= 1;
				m_bBtnDown	= true;
				SetCapture(m_hParent);
				Redraw();
			}
			break;
		}
		case WM_LBUTTONUP:
		{
			if (m_bBtnDown)
			{
				bHandle		= true;
				m_nCurSel	= 0;
				m_bBtnDown	= false;
				ReleaseCapture();
				Redraw();
				if (CursorInCtrl(lParam))
				{
					SendMessage(m_hParent, WM_COMMAND, MAKEWPARAM(m_nCtrlID, BM_CLICK), 0);
				}
			}

			break;
		}
	}

	return bHandle;
}
